package com.luoye.lj.platform.controller;

import com.luoye.lj.platform.pojo.vo.ManageMenuVo;
import com.luoye.lj.platform.pojo.vo.ManageMenuVoCollection;
import com.luoye.lj.platform.pojo.dto.ManageMenuDtoSearchCondition;
import com.luoye.lj.platform.pojo.dto.ManageMenuDto;
import com.luoye.lj.platform.pojo.dbo.ManageMenuDbo;
import com.luoye.lj.platform.pojo.dbo.ManageMenuDboCollection;
import com.luoye.lj.platform.pojo.dbo.ManageMenuDboSearchCondition;
import com.luoye.lj.platform.service.IManageMenuService;

import com.luoye.lj.util.converter.PojoConvertUtil;
import com.luoye.lj.util.mvc.annotation.OperateLog;
import com.luoye.lj.util.mvc.data.SimpleListResult;
import com.luoye.lj.util.mvc.data.SimpleResponseBody;
import com.luoye.lj.util.mvc.log.DefaultDescriptionGenerator;
import com.luoye.lj.util.mvc.util.PageService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User:
 * Date:
 * Time:
 * To change this template use File | Settings | File Templates.
 */

@Slf4j
@Controller
@RequestMapping("/system/manageMenu")
public class ManageMenuController {

    @Autowired
    private IManageMenuService manageMenuService;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(double.class,new PropertyEditorSupport(){
            @Override
            public void setAsText(String text) throws java.lang.IllegalArgumentException {
                log.info("PropertyEditorSupport");
                double value=-1;
                try{
                    value=Double.parseDouble(text);
                }catch (NumberFormatException ex){
                    //ex.printStackTrace();
                }
                setValue(value);
            }
        });
        binder.registerCustomEditor(int.class,new PropertyEditorSupport(){
            @Override
            public void setAsText(String text) throws java.lang.IllegalArgumentException {
                log.info("PropertyEditorSupport");
                int value=-1;
                try{
                    value=Integer.parseInt(text);
                }catch (NumberFormatException ex){
                    //ex.printStackTrace();
                }
                setValue(value);
            }
        });
        binder.registerCustomEditor(long.class,new PropertyEditorSupport(){
            @Override
            public void setAsText(String text) throws java.lang.IllegalArgumentException {
                log.info("PropertyEditorSupport");
                long value=-1;
                try{
                    value=Long.parseLong(text);
                }catch (NumberFormatException ex){
                    //ex.printStackTrace();
                }
                setValue(value);
            }
        });
        binder.registerCustomEditor(float.class,new PropertyEditorSupport(){
            @Override
            public void setAsText(String text) throws java.lang.IllegalArgumentException {
                log.info("PropertyEditorSupport");
                float value=-1;
                try{
                    value=Float.parseFloat(text);
                }catch (NumberFormatException ex){
                    //ex.printStackTrace();
                }
                setValue(value);
            }
        });
        binder.registerCustomEditor(Date.class,new PropertyEditorSupport(){
            @Override
            public void setAsText(String text) throws java.lang.IllegalArgumentException {
                log.info("PropertyEditorSupport");
                Date value=null;
                try {
                    value=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(text);
                } catch (ParseException e) {
                    //e.printStackTrace();
                }
                setValue(value);
            }
        });
    }

    @RequestMapping("/list")
    public String list(HttpServletRequest request,Model model){
        model.addAttribute("title","菜单管理");
        //查出一级菜单
        ManageMenuDtoSearchCondition manageMenuDtoSearchCondition=new ManageMenuDtoSearchCondition();
        manageMenuDtoSearchCondition.setParentId(0);
        manageMenuDtoSearchCondition.setMax(Integer.MAX_VALUE);
        log.info("manageMenuDtoSearchCondition->" + manageMenuDtoSearchCondition);
        ManageMenuDboSearchCondition manageMenuDboSearchCondition= PojoConvertUtil.convertPojo(manageMenuDtoSearchCondition,ManageMenuDboSearchCondition.class);
        ManageMenuDboCollection manageMenuDboCollection=manageMenuService.searchByCondition(manageMenuDboSearchCondition);
        model.addAttribute("menus",manageMenuDboCollection.getEntityList());

        return "system/manageMenu/manageMenu_list";
    }
    @RequestMapping("/search")
    @ResponseBody
    public SimpleListResult search(ManageMenuDtoSearchCondition manageMenuDtoSearchCondition,@RequestParam(value = "page", defaultValue = "1") int page){
        int offset = (page - 1) * manageMenuDtoSearchCondition.getMax();
        manageMenuDtoSearchCondition.setOffset(offset);
        log.info("manageMenuDtoSearchCondition->" + manageMenuDtoSearchCondition);

        ManageMenuDboSearchCondition manageMenuDboSearchCondition= PojoConvertUtil.convertPojo(manageMenuDtoSearchCondition,ManageMenuDboSearchCondition.class);

        SimpleListResult slr = new SimpleListResult();
        ManageMenuDboCollection manageMenuDboCollection=manageMenuService.searchByCondition(manageMenuDboSearchCondition);
        ManageMenuVoCollection manageMenuVoCollection=PojoConvertUtil.convertPojo(manageMenuDboCollection,ManageMenuVoCollection.class);

        if(manageMenuVoCollection != null) {
            slr.setRows(manageMenuVoCollection.getEntityList());
            slr.setCurrentPage(page);
            slr.setResults(manageMenuVoCollection.getTotalCount());
            slr.setPages(PageService.countPages(manageMenuDtoSearchCondition.getMax(), slr.getResults()));
        }

        return slr;
    }


    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    @OperateLog(value = "菜单信息添加",descriptionGenerator = DefaultDescriptionGenerator.class)
    public SimpleResponseBody add(HttpServletRequest request,ManageMenuDto manageMenuDto){
        SimpleResponseBody<ManageMenuVo> srb=new SimpleResponseBody<ManageMenuVo>();
        srb.setOk(true);
        try {
            ManageMenuDbo manageMenuDbo=PojoConvertUtil.convertPojo(manageMenuDto,ManageMenuDbo.class);
            ManageMenuDbo manageMenuBack=manageMenuService.insert(manageMenuDbo);
            ManageMenuVo manageMenuVo=PojoConvertUtil.convertPojo(manageMenuBack,ManageMenuVo.class);
            srb.setEntity(manageMenuVo);
        }catch (Exception ex){
            log.error("" + ex.getMessage(), ex);
            srb.setOk(false);
            srb.setMsg("发生了未知错误");
        }
        return srb;
    }

    @RequestMapping(value = "/modify",method = RequestMethod.POST)
    @ResponseBody
    @OperateLog(value = "菜单信息修改",descriptionGenerator = DefaultDescriptionGenerator.class)
    public SimpleResponseBody modify(HttpServletRequest request,ManageMenuDto manageMenuDto){
        SimpleResponseBody<Integer> srb=new SimpleResponseBody<Integer>();
        srb.setOk(true);
        try {
            ManageMenuDbo manageMenuDbo=PojoConvertUtil.convertPojo(manageMenuDto,ManageMenuDbo.class);
            int count=manageMenuService.update(manageMenuDbo);
            srb.setEntity(new Integer(count));
        }catch (Exception ex){
            log.error("" + ex.getMessage(), ex);
            srb.setOk(false);
            srb.setMsg("发生了未知错误");
        }
        return srb;
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    @OperateLog(value = "菜单信息删除",descriptionGenerator = DefaultDescriptionGenerator.class)
    public SimpleResponseBody delete(HttpServletRequest request,int id){
         SimpleResponseBody<Integer> srb=new SimpleResponseBody<Integer>();
        srb.setOk(true);
        try {
            int count=manageMenuService.delete(id);
            srb.setEntity(new Integer(count));
        }catch (Exception ex){
            log.error("" + ex.getMessage(), ex);
            srb.setOk(false);
            srb.setMsg("发生了未知错误");
        }
        return srb;
    }
}
